package com.qianfeng.openapi.web.master.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qianfeng.openapi.web.master.bean.AjaxMessage;
import com.qianfeng.openapi.web.master.pojo.AdminUser;
import com.qianfeng.openapi.web.master.pojo.Menu;
import com.qianfeng.openapi.web.master.service.AdminUserService;
import com.qianfeng.openapi.web.master.service.MenuService;
import com.qianfeng.openapi.web.master.util.AdminConstants;
import com.qianfeng.openapi.web.master.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class SystemController {
    @Autowired
    private MenuService menuService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;


    @RequestMapping("/logout")
    public String logout(@CookieValue(value = AdminConstants.COOKIE_SESSION_USER_KEY) String cookie) {
        // 移除用户的认证信息
        redisTemplate.delete(AdminConstants.SESSION_USER + cookie);
        // 移除用户的权限信息
        redisTemplate.delete(AdminConstants.USER_MENU + cookie);
        // 跳转到登录页面
        return "login.html";
    }

    @RequestMapping("/dologin")
    @ResponseBody
    public AjaxMessage login(String email, String password,HttpServletResponse resp) throws JsonProcessingException {
        AdminUser user = adminUserService.doLogin(email, password);
        if (user == null) {
            return new AjaxMessage(false);
        }
        // 将用户信息放到Redis
        //1.1 生成一个唯一的key
        String key = UUID.randomUUID().toString();
        //1.2 将用户信息存储到Redis,并设置生存时间为30分钟
        redisTemplate.opsForValue().set(AdminConstants.SESSION_USER + key,objectMapper.writeValueAsString(user),AdminConstants.SESSION_TIMEOUT, TimeUnit.SECONDS);



        List<Menu> menuList = menuService.getUserMenuList(user.getId());
        // 将菜单信息放到Redis
        //2.1 将菜单信息放存储到Redis,并设置生存时间为30分钟
        redisTemplate.opsForValue().set(AdminConstants.USER_MENU + key,objectMapper.writeValueAsString(menuList),AdminConstants.SESSION_TIMEOUT, TimeUnit.SECONDS);


        // 将Redis中的key通过cookie的形式返回给浏览器端
        //3.1 声明resp
        //3.2 创建Cookie对象,并指定生存时间...
        Cookie cookie = new Cookie(AdminConstants.COOKIE_SESSION_USER_KEY,key);
        cookie.setMaxAge(99999999);
        //3.2 使用resp写回cookie
        resp.addCookie(cookie);

        return new AjaxMessage(true);
    }

    @RequestMapping("/auth_error")
    public String error() {
        return "error.html";
    }

    @RequestMapping("/side")
    @ResponseBody
    public AjaxMessage getMenuTree(@CookieValue(AdminConstants.COOKIE_SESSION_USER_KEY) String key, HttpServletResponse response) {
        // 从Redis中获取用户信息
        String adminUserJson = redisTemplate.opsForValue().get(AdminConstants.SESSION_USER + key);
        AdminUser user = (AdminUser) JsonUtil.getObj(adminUserJson, AdminUser.class);
        if (user == null) {
            try {
                response.sendRedirect("/login.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new AjaxMessage(true, null, new ArrayList<>());
        }
        List<Menu> menus = menuService.getUserPermission(user.getId());
        return new AjaxMessage(true, null, menus);
    }
}
